package cn.antho.org.controller;

import cn.antho.basic.anno.PreAuthorize;
import cn.antho.basic.config.BussinessException;
import cn.antho.basic.util.ExcelUtil;
import cn.antho.org.domain.ShopAuditLog;
import cn.antho.org.service.IShopService;
import cn.antho.org.domain.Shop;
import cn.antho.org.query.ShopQuery;
import cn.antho.basic.util.AjaxResult;
import cn.antho.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
@PreAuthorize
public class ShopController {

    @Autowired
    public IShopService IShopService;

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId() != null)
                IShopService.update(shop);
            else
                IShopService.add(shop);
            return AjaxResult.me().success();
        } catch (BussinessException bx) {
            return AjaxResult.me().fail(bx.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "根据id删除", sn = "shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            IShopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    // 获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据id查询店铺",sn = "shop:get")
    public Shop get(@PathVariable("id") Long id) {
        return IShopService.findById(id);
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return IShopService.findAll();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "分页查询店铺",sn = "shop:json")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return IShopService.queryByPage(query);
    }

    /**
     * 店铺入驻接口
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid Shop shop) {
        IShopService.settlement(shop);
        return AjaxResult.me().success();
    }

    /**
     * 店铺入驻审核
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) {
        IShopService.auditPass(shopAuditLog);
        return AjaxResult.me().success();
    }

    /**
     * 店铺激活
     * @param id
     * @return
     */
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) {
        IShopService.active(id);
        return AjaxResult.me().success();
    }

    /**
     * 驳回后再入驻
     */
    @PostMapping("/settlement/again")
    public AjaxResult settlementAgain(@RequestBody Shop shop){
        shop.setState(0);
        IShopService.update(shop);
        return AjaxResult.me().success();
    }

    /**
     * Excel导出
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> all = IShopService.findAll();
        ExcelUtil.exportExcel(all,"","sheet1",Shop.class,"店铺列表.xlsx",response);
    }

    /**
     * Excel导入
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 2, Shop.class);
        IShopService.addAll(shops);
    }

    /**
     * Echarts报表
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        return IShopService.echarts();
    }

    @PatchMapping
    @PreAuthorize(name = "批量删除店铺",sn = "shop:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        IShopService.batchDel(ids);
        return AjaxResult.me().success();
    }

}
